﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace HomeDmsUtils
{
    public static class GraphicsHelper
    {
        public static Image BuildThumbnailImage(this Image originalImage, Size size)
        {
            Size zsize = GetZoomedSize(originalImage, size);
            if (zsize.IsEmpty)
                return null;
            PixelFormat format;
            //switch (originalImage.PixelFormat)
            //{
            //    case PixelFormat.Format1bppIndexed : 
            //        format = PixelFormat.
            //}
            Image thumb = new Bitmap(zsize.Width, zsize.Height, /*originalImage.PixelFormat*/PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(thumb);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            Rectangle rect = new Rectangle(Point.Empty, zsize);
            g.DrawImage(originalImage, rect);

            return thumb;
        }

        public static Image BuildThumbnailImage(this Image originalImage, SizeF size)
        {
            SizeF zsize = GetZoomedSize(originalImage, size);
            if (zsize.IsEmpty)
                return null;
            PixelFormat format;
            //switch (originalImage.PixelFormat)
            //{
            //    case PixelFormat.Format1bppIndexed : 
            //        format = PixelFormat.
            //}
            Size zsizePix = new Size(
                (int)Math.Round(zsize.Width * originalImage.HorizontalResolution), 
                (int)Math.Round(zsize.Height * originalImage.VerticalResolution));
            Image thumb = new Bitmap(zsizePix.Width, zsizePix.Height, /*originalImage.PixelFormat*/PixelFormat.Format32bppArgb);
            //thumb.HorizontalResolution = originalImage.HorizontalResolution;
            //thumb.VerticalResolution = originalImage.VerticalResolution;
            Graphics g = Graphics.FromImage(thumb);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            RectangleF rect = new RectangleF(PointF.Empty, zsizePix);
            g.DrawImage(originalImage, rect);

            return thumb;
        }

        public static Size GetZoomedSize(this Image image, Size clientSize)
        {            
            float Wrate = (float)clientSize.Width / image.Size.Width;
            float Hrate = (float)clientSize.Height / image.Size.Height;
            float rate = Math.Min(Wrate, Hrate);
            Size size = new Size();
            size.Width = (int)Math.Round(image.Width * rate);
            size.Height = (int)Math.Round(image.Height * rate);
            return size;
        }

        public static Size GetZoomedSizeForPrinter(this Image image, Size clientSize, float xDpi, float yDpi)
        {
            // printer unit is inch/100
            float Wrate = ((float)clientSize.Width / 100f) * xDpi / image.Size.Width;
            float Hrate = ((float)clientSize.Height / 100f) * yDpi / image.Size.Height;
            float rate = Math.Min(Wrate, Hrate);
            Size size = new Size();
            size.Width = (int)Math.Round(image.Width * rate / xDpi * 100);
            size.Height = (int)Math.Round(image.Height * rate / yDpi * 100);
            return size;
        }

        public static SizeF GetZoomedSize(this Image image, SizeF clientSize)
        {
            float imgWidth = image.Size.Width / image.HorizontalResolution;
            float imgHeight = image.Size.Height / image.VerticalResolution;
            float Wrate = clientSize.Width / imgWidth; // in inch
            float Hrate = clientSize.Height / imgHeight; // in inch
            float rate = Math.Min(Wrate, Hrate);
            SizeF size = new SizeF();
            size.Width = imgWidth * rate;
            size.Height = imgHeight * rate;
            return size;
        }
    }
}
